﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MetaProperties.Collections
{
    using System.Collections;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;

    public partial class ObservableList<ItemT>
    {
        /// <summary>
        /// Clears this instance.
        /// </summary>
        public void Clear()
        {
            // NB: The items.Clear() method raises a Reset event for clear, which isn't
            // very helpful if we need to tidy up.  We'll raise a Remove event instead.
            if (this.items.Count != 0)
            {
                NotifyCollectionChangedEventArgs args = null;
                this.SurpressChangedEvents = true;
                try
                {
                    IList oldItems = new ArrayList(this.items);

                    this.items.Clear();

                    args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItems, 0);
                }
                finally
                {
                    this.SurpressChangedEvents = false;
                }

                this.Items_CollectionChanged(this, args);
            }
        }
    }
}
